package com.zhl.media.app.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.jess.arms.di.component.AppComponent;
import com.zhl.media.app.R;
import com.zhl.media.app.greendao.MediaPhotoDao;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonres.view.PublicTitleBar;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.app.model.MediaPosition;
import butterknife.BindView;
@Route(path = RouterHub.APP_LOCATIONACTIVITY)
public class LocationActivity extends AppActivity {
    private BaiduMap mBaiduMap;
    private MediaPosition media;
    private MediaPhotoDao mediaPhotoDao;
    @BindView(R.id.addr)
    TextView addr;
    @BindView(R.id.bmap_View)
    MapView mMapView;
    @BindView(R.id.titleBar)
    PublicTitleBar publicTitleBar;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_location;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        Intent i = getIntent();
        media = (MediaPosition)i.getSerializableExtra("media");
        publicTitleBar.setTitle("位置信息");
        mediaPhotoDao = EntityManager.getInstance(this).getMediaPhotoDao();
        init();
    }

    private void init() {
        MediaPhoto unique = mediaPhotoDao.queryBuilder().where(MediaPhotoDao.Properties.MediaId.eq(media.getId())).unique();
        mBaiduMap = mMapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
        mBaiduMap.setMapStatus(msu);
        mMapView.setLongClickable(true);
        int count = mMapView.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = mMapView.getChildAt(i);
            if (child instanceof ImageView || child instanceof ZoomControls) {
                child.setVisibility(View.INVISIBLE);
            }
        }
        mMapView.showScaleControl(false);
        LatLng p = new LatLng(unique.getLatitude(), unique.getLongitude());
        mMapView = new MapView(this, new BaiduMapOptions().mapStatus(new MapStatus.Builder().target(p).build()));
        showMap(unique.getLatitude(),  unique.getLongitude(),"");
    }
    private void showMap(double latitude, double longtitude, String address) {
        LatLng llA = new LatLng(latitude, longtitude);
        OverlayOptions ooA = new MarkerOptions().position(llA).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_yourself_lication)).zIndex(4).draggable(true);
        mBaiduMap.addOverlay(ooA);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(llA, 20.0f);
        mBaiduMap.animateMapStatus(u);
        addr.setText("拍摄地址："+address);
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }


}
